<?php
class Helper_Utils extends Zend_Controller_Action_Helper_Abstract
{   
    private static $disalowedtags = array("script",
                        "object",
                        "iframe",
                        "image",
                        "applet",
                        "meta",
                        "form",
                        "onmouseover",
                        "onmouseout");
                        
	static function stripTags($text, $tags = array()){
		$args = func_get_args();
        $text = array_shift($args);
        $tags = func_num_args() > 2 ? array_diff($args,array($text))  : (array)$tags;
        if(count($tags) <= 0)
        	$tags = Helper_Utils::$disalowedtags;
        foreach ($tags as $tag){
            if(preg_match_all('/<'.$tag.'[^>]*>(.*)<\/'.$tag.'>/iU', $text, $found)){
                $text = str_replace($found[0],$found[1],$text);
          }
        }

        return $text;
	}
	static function checkEmail($email){
		$pattern = '/^(([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+([;.](([a-zA-Z0-9_\-\.]+)@([a-zA-Z0-9_\-\.]+)\.([a-zA-Z]{2,5}){1,25})+)*$/';
		if(preg_match($pattern, $email))
			return true;
		else
			return false;
	}
	
	static function checkWebsite($url){
		$pattern = "/^((https?|ftp)\:\/\/)?[a-z0-9-]+(\.[a-z0-9-]+)*(:[0-9]+)?(\/.*)?$/";
		if(preg_match($pattern, $url))
			return true;
		else
			return false;
	}
	function check_plain($text) {
	  return htmlspecialchars($text, ENT_QUOTES);
	}
	
    public function getDataInfo($str_data) {
		$arr_data = explode(';#', $str_data);
		
		if (!isset($arr_data[1])) $arr_data[1] = 0;
		if (!isset($arr_data[2])) $arr_data[2] = 1;
		
		return $arr_data;
	}
	
    public function setDataInfo($arr_data, $type = 0) {
    	if ($type == 0)
    		$arr_data[2] = ($arr_data[2] == 'on') ? 1 : 0;
    	elseif ($type == 1)
    		$arr_data[3] = ($arr_data[3] == 'on') ? 1 : 0;

		$str_data = implode(';#', $arr_data);
		
		return $str_data;
	}
	
    public function viewPriority($priority = 0, $is_friend, $is_logged)
	{
		if ($priority == 3)
				return false;
		elseif ($priority == 2)
			if ($is_friend)
				return true;
			else
				return false;
		elseif ($priority == 1)
			if ($is_logged) 
				return true;
			else
				return false;
		else
			return true;	
	}
	
	/**
	 * Verify the syntax of the given string.
	 */
	public function checkString($str) {
	  if (preg_match('/[^\x80-\xF7 [:alnum:]@_.-]/', $str)) return false;
	  
	  if (preg_match('/[\x{80}-\x{A0}'.          // Non-printable ISO-8859-1 + NBSP
	                   '\x{AD}'.                 // Soft-hyphen
	                   '\x{2000}-\x{200F}'.      // Various space characters
	                   '\x{2028}-\x{202F}'.      // Bidirectional text overrides
	                   '\x{205F}-\x{206F}'.      // Various text hinting characters
	                   '\x{FEFF}'.               // Byte order mark
	                   '\x{FF01}-\x{FF60}'.      // Full-width latin
	                   '\x{FFF9}-\x{FFFD}'.      // Replacement characters
	                   '\x{0}]/u',               // NULL byte
	                   $str)) {
		return false;
	  }  

	  if (mb_strlen($str) > 25) return false;
	  
	  return true;
	}
	
	//Image function
	public function imageGetInfo($file) {
	  if (!is_file($file)) {
	    return FALSE;
	  }

	  $details = FALSE;
	  $data = @getimagesize($file);
	  $file_size = @filesize($file);

	  if (isset($data) && is_array($data)) {
	    $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
	    $extension = array_key_exists($data[2], $extensions) ?  $extensions[$data[2]] : '';
	    $details = array('width'     => $data[0],
	                     'height'    => $data[1],
	                     'extension' => $extension,
	                     'file_size' => $file_size,
	                     'mime_type' => $data['mime']);
	  }

	  return $details;
	}
	
	/**
	* GD helper function to create an image resource from a file.
	*/
	public function imageGDopen($file, $extension) {
		$extension = str_replace('jpg', 'jpeg', $extension);
		$open_func = 'imageCreateFrom'. $extension;
		if (!function_exists($open_func)) {
			return FALSE;
		}
		return $open_func($file);
	}
	
	/**
	 * GD helper to write an image resource to a destination file.
	 */
	public function imageGDclose($res, $destination, $extension) {
		$extension = str_replace('jpg', 'jpeg', $extension);
		$close_func = 'image'. $extension;
		if (!function_exists($close_func)) {
		return FALSE;
		}
		if ($extension == 'jpeg') {
		return $close_func($res, $destination, AVATAR_QUALITY);
		}
		else {
		return $close_func($res, $destination);
		}
	}		
	
	/**
	 * Scale an image to the specified size using GD.
	 */
	public function imageResize($source, $destination, $width, $height) {
		if (!file_exists($source)) {
			return FALSE;
		}

		$info = $this->imageGetInfo($source);
		if (!$info) {
			return FALSE;
		}

		$im = $this->imageGDopen($source, $info['extension']);
		if (!$im) {
			return FALSE;
		}

		$res = imageCreateTrueColor($width, $height);
		if ($info['extension'] == 'png') {
		$transparency = imagecolorallocatealpha($res, 0, 0, 0, 127);
		imagealphablending($res, FALSE);
		imagefilledrectangle($res, 0, 0, $width, $height, $transparency);
		imagealphablending($res, TRUE);
		imagesavealpha($res, TRUE);
		}
		imageCopyResampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']);
		$result = $this->imageGDclose($res, $destination, $info['extension']);

		imageDestroy($res);
		imageDestroy($im);

		return $result;
	}	
    
    function arr2Url ( $a ) {
        if (!is_array($a)) return "";
        $s = array();
        foreach ( $a as $key => $value ) {
            $s[] = $key."=".$value;
        }
        return implode ("&", $s);
    }  
  
    public function TruncateUTF8($string, $len, $wordsafe = FALSE, $dots = FALSE) {
      $slen = strlen($string);
      if ($slen <= $len) {
        return $string;
      }
      if ($wordsafe) {
        $end = $len;
        while (($string[--$len] != ' ') && ($len > 0)) {};
        if ($len == 0) {
          $len = $end;
        }
      }
      if ((ord($string[$len]) < 0x80) || (ord($string[$len]) >= 0xC0)) {
        return substr($string, 0, $len) . ($dots ? ' ...' : '');
      }
      while (--$len >= 0 && ord($string[$len]) >= 0x80 && ord($string[$len]) < 0xC0) {};
      
      return substr($string, 0, $len) . ($dots ? ' ...' : '');
   }  
}


